package masterjava.admingui.shared.entity;

import masterjava.admingui.shared.RoleType;
import masterjava.common.shared.util.SerializeUtil;
import org.jetbrains.annotations.Nullable;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: Grigory Kislin
 * Date: 23/06/11
 */
@Entity
@Table(name = "USER")
@NamedQueries({
        @NamedQuery(
                name = User.FIND_BY_LOGIN,
                query = "from User u where u.login=:" + User.LOGIN
        )
})

@Access(AccessType.PROPERTY)
public class User extends Client {

    public User() {
    }

    public User(String login, String password, String firstName, String lastName, Partner partner, RoleType role, String email, List<Phone> phones) {
        this(login, password, firstName, null, lastName, partner, role, email, null, null, null, null, null, phones);
    }

    public User(String login, String password, String firstName, @Nullable String middleInitial, String lastName, Partner partner, RoleType role, String email, @Nullable String address, @Nullable Date startDate, @Nullable Date endDate, @Nullable String comments, @Nullable Attachment logo, List<Phone> phones) {
        super(role, email, address, startDate, endDate, comments, logo);
        this.phones = phones;
        this.login = login;
        this.password = password;
        this.firstName = firstName;
        this.middleInitial = middleInitial;
        this.lastName = lastName;
        this.partner = partner;
    }

    public static final String FIND_BY_LOGIN = "FIND_BY_LOGIN";
    public static final String LOGIN = "login";

    private List<Phone> phones = new ArrayList<Phone>();

    private String login;

    private String password;

    private String firstName;

    private String middleInitial;

    private String lastName;

    // do not serialized
    transient private Partner partner;

    // do not persisted
    private Long partnerId = null;

    // do not persisted
    private String fullName = null;

    @Column(name = "FIRST_NAME")
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @Column(name = "MIDDLE_INITIAL")
    public String getMiddleInitial() {
        return middleInitial;
    }

    public void setMiddleInitial(String middleInitial) {
        this.middleInitial = middleInitial;
    }

    @Column(name = "LAST_NAME")
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "PARTNER_ID", nullable = true)
    public Partner getPartner() {
        return partner;
    }

    public void setPartner(Partner partner) {
        this.partner = partner;
    }

    @Column(name = "LOGIN", nullable = false, unique = true)
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Column(name = "PASSWORD")
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "user", cascade = CascadeType.ALL)
    public List<Phone> getPhones() {
        return phones;
    }

    public void setPhones(List<Phone> phones) {
        this.phones = phones;
    }

    @Transient
    public String getFullName() {
        return fullName;
    }

    @Transient
    public Long getPartnerId() {
        return partnerId;
    }

    @Override
    public void prepareToPersist() {
        if (phones != null) {
            for (Phone phone : phones) {
                phone.setUser(this);
            }
        }
    }

    @Override
    public void prepareToSerialize() {
        super.prepareToSerialize();
        fullName = firstName + " " + lastName;
        phones = SerializeUtil.getSerializedCollection(phones);
        partnerId = evalEntityId(partner);
    }
}
